package io;

import java.io.FileInputStream;
import java.util.Date;
import com.alibaba.druid.pool.DruidDataSource;
import com.such.kit.datacast.CastCommons;
import com.such.kit.io.template.JSONIOTemplate;
import com.such.kit.io.source.DatabaseIOSource;
import com.such.kit.io.source.MemoryIOSource;
import com.such.kit.io.template.support.IOTemplate;

public class JSONIO {

	private DatabaseIOSource databaseIOSource;
	private MemoryIOSource memoryIOSource;

	public JSONIO() throws Exception {
		/*
		DruidDataSource dataSource = new DruidDataSource();
		dataSource.setDriverClassName("oracle.jdbc.driver.OracleDriver");
		dataSource.setUrl("jdbc:oracle:thin:@10.0.1.169:1521:VMS");
		dataSource.setUsername("CRSDEVELOP");
		dataSource.setPassword("CRSDEVELOP");
		this.databaseIOSource = new DatabaseIOSource(dataSource);
		*/
		this.memoryIOSource = new MemoryIOSource();
	}

	public void ioTemplate() throws Exception {
		String basePath = getClass().getResource("").getPath();

		/*
		IOTemplate jsonIO = new JSONIO(new FileInputStream(basePath + "jsonTemplate.txt"), databaseIOSource);
		jsonIO.output(basePath, "output");
		*/

		JSONIOTemplate jsonIO = new JSONIOTemplate(new FileInputStream(basePath + "jsonTemplate.txt"));
		// jsonIO.setRootType(JSONIOTemplate.ROOT_TYPE_OBJECT);
		jsonIO.input(this.memoryIOSource, basePath + "jsonResource.txt");
		for (String key : memoryIOSource.getInputKeys()) {
			System.out.println(key + " > " + memoryIOSource.getInputResult(key));
		}
	}

	public static void main(String[] args) throws Exception {
		JSONIO jsonIO = new JSONIO();
		System.out.println("开始：" + CastCommons.dateToString(new Date(), "yyyy-MM-dd HH:mm:ss"));
		jsonIO.ioTemplate();
		System.out.println("完成：" + CastCommons.dateToString(new Date(), "yyyy-MM-dd HH:mm:ss"));
	}

}
